package com.soft.collateral.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soft.collateral.constant.MessageConstant;
import com.soft.collateral.entity.StorageApply;
import com.soft.collateral.entity.StorageApproval;
import com.soft.collateral.entity.WithdrawApplyDetail;
import com.soft.collateral.result.Result;
import com.soft.collateral.service.StorageService;
import com.soft.collateral.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/storage")
@Api(tags = "后台系统-入库管理接口")
@RequiredArgsConstructor
public class StorageController {

    private final StorageService storageService;

    @ApiOperation("查询入库申请列表")
    @GetMapping("list")
    public Result list() {
        List<StorageApply> storageApplyList = storageService.listAll();
        if (!storageApplyList.isEmpty()) {
            return new Result(true, MessageConstant.QUERY_STORAGE_SUCCESS, storageApplyList);
        }
        return new Result(false, MessageConstant.QUERY_STORAGE_FAIL);
    }

    @ApiOperation("条件分页查询入库申请列表")
    @GetMapping("page")
    public Result page(PageQueryVO pageQueryVO) {
        Page<StorageApply> pageQuery = storageService.pageQuery(pageQueryVO);
        if (pageQuery != null) {
            return new Result(true, MessageConstant.QUERY_STORAGE_SUCCESS, pageQuery);
        }
        return new Result(false, MessageConstant.QUERY_STORAGE_FAIL);
    }

    @ApiOperation("新增入库申请")
    @PostMapping
    public Result create(@RequestBody StorageApply storageApply) {
        boolean success = storageService.saveStorageApply(storageApply);
        if (success) {
            return new Result(true, MessageConstant.ADD_STORAGE_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_STORAGE_FAIL);
    }

    @ApiOperation("通过ID查询入库申请信息")
    @GetMapping("{id}")
    public Result findById(@PathVariable Integer id) {
        StorageApply storageApply = storageService.getByIdWithDetails(id);
        if (storageApply != null) {
            return new Result(true, MessageConstant.QUERY_STORAGE_SUCCESS, storageApply);
        }
        return new Result(true, MessageConstant.QUERY_STORAGE_SUCCESS);
    }

    @ApiOperation("编辑入库申请")
    @PutMapping
    public Result update(@RequestBody StorageApply storageApply) {
        boolean success = storageService.updateStorageApply(storageApply);
        if (success) {
            return new Result(true, MessageConstant.EDIT_STORAGE_SUCCESS);
        }
        return new Result(false, MessageConstant.EDIT_STORAGE_FAIL);
    }

    @ApiOperation("删除入库申请")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean success = storageService.removeByIdWithDetails(id);
        if (success) {
            return new Result(true, MessageConstant.DELETE_STORAGE_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_STORAGE_FAIL);
    }

    @ApiOperation("审核入库申请")
    @PostMapping("approve")
    public Result approve(@RequestBody StorageApproval approval) {
        boolean success = storageService.approveStorageApply(approval);
        if (success) {
            return new Result(true, MessageConstant.APPROVE_STORAGE_SUCCESS);
        }
        return new Result(false, MessageConstant.APPROVE_STORAGE_FAIL);
    }
}